{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2162faa3-c128-4588-8627-18c687f29a72",
   "metadata": {},
   "source": [
    "Requirements\n",
    "You can assume you will be given an integer input.\n",
    "\n",
    "You can not assume that the integer will be only positive. \n",
    "You may be given negative numbers as well ( or ).0\n",
    "\n",
    "NOTE on performance: There are no fancy optimizations required,\n",
    " but still the most trivial solutions might time out. \n",
    " Numbers go up to 2^31 ( or similar, depending on language ). \n",
    " Looping all the way up to , or , will be too slow.nn/2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00856f2f-430c-47f6-baf3-a4c93121d6ce",
   "metadata": {},
   "source": [
    "定义一个函数，该函数采用整数参数并返回逻辑值，或者取决于整数是否为质数。true or false\n",
    "根据维基百科，素数（或质数）是一个大于1的自然数，除了1 和 本身之外没有正约数。\n",
    "要求\n",
    "您可以假设您将获得一个整数输入。\n",
    "您不能假设整数只是正数。您也可能得到负数 或 0\n",
    "性能说明：不需要花哨的优化，但最微不足道的解决方案仍然可能会超时。\n",
    "数字最高可达 2^31（或类似，取决于语言）。一直循环到 n，或 n/2，会太慢。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3471a7f0-9b0c-4352-8cfb-358e426b9c32",
   "metadata": {},
   "outputs": [],
   "source": [
    "python 典中典例题：判断素数（质数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "57a248bc-856c-4f69-851c-1dd61949db95",
   "metadata": {},
   "outputs": [],
   "source": [
    "def xx1(n):\n",
    "    if n < 2:\n",
    "        return False\n",
    "    for i in range(2,int(n**.5)+1):\n",
    "        if n % i == 0:\n",
    "            return False\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6a5dd017-248d-4d04-876e-53568b3749a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2, 3, 5, 7, 11, 13, 17, 19, "
     ]
    }
   ],
   "source": [
    "for i in range(1,21):\n",
    "    if xx1(i):\n",
    "        print(i, end=', ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c1ea4ae4-2492-4cc9-97ff-636b4229d886",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (2367938482.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[30], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    [i for i in range(1,21) xx1(i)]\u001b[0m\n\u001b[1;37m                            ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "[i for i in range(1,21) xx1(i)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1d3b61f-ee17-45da-bb69-984ecfe9d9bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def xx2(n):\n",
    "    return n>1 and len([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb81d385-0d81-43e4-ac15-7cca74796b0d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b81eb4f-0822-4bdd-a04c-c9d03cb0dec9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "340a8176-e9ab-4713-affd-ea3ea6071d00",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97bb2be0-82e7-466e-866a-a37039e5180f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bc18844-0fa7-4f9a-a50d-d93174e615e3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1d42998-8c6b-4030-9003-c7fbeb480be2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "023ec754-4be9-42f7-af3f-187f001d362d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "447ae014-aee5-4b39-b363-e9ae48e878c3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93b0f33c-89e0-4e5f-9d93-5861090f6a0c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9a13336-f8d5-4fe1-aee6-558e119bc3fb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82b5318b-cdb7-4cbf-8a93-43b728d8cc61",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "306fe4f9-838c-4dac-ab50-9d0c5e489ef3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "486b5257-67c2-4d8b-83a5-e021b9702eff",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f39f12c1-8854-4b2c-8c6a-03dd18fe8488",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "592f60c7-bf07-40ce-ab7a-d3271e825fe8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02b7ea6b-3ffb-4371-add1-2a8d76601c27",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be0f5feb-5cc3-49e6-bf2b-c9b9c254b1c6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ead8af0d-0025-4a94-a70f-562d91d7bdb0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4631b75-ce66-4363-a26a-030eae82e8ad",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f4cb64e-5c87-4bf4-8e1c-cfdf90b7cee5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a58e56f4-ca53-40f6-bf19-b432fa97384f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a275c464-ede3-4272-b6fc-fc91515b0cc4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4437007f-2324-4504-82f7-918e01c23cfb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "baee382f-0d66-4c02-bc16-879e0b4d1e30",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7f55be9-a18d-4dca-a9eb-10f7738f0726",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3240c3bd-34ad-429f-b0fa-86173855f70e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f662e366-01ea-4e47-b98a-6dd0f3e5d246",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30995411-69f6-411e-a324-cf5bda79a8bb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a5c6aaf-590c-4856-a772-563b750ede81",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0408072b-dddd-4da7-b6dd-a138053f69f3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a6acb2d-9993-4de8-9ee5-dba9e125d918",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5a0b5fd-6751-4aeb-b55e-18f9edeae15a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "769df500-a1ce-47ff-9b17-3c2b349071ac",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ce170644-ee76-425d-884d-edb1a7f9b24c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def xx1(n):\n",
    "    if n < 2: return False\n",
    "    for i in range(2,int(n**.5)+1):\n",
    "        if n%i ==0:\n",
    "            return False\n",
    "    return True           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fba7806e-2ee5-4712-8cc5-6f101982df6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def xx2(n):\n",
    "    return n>1 and len([i for i in range(2,int(n**.5)+1) if n%i==0]) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5e441706-236f-41a7-91d0-f1cc9503489c",
   "metadata": {},
   "outputs": [],
   "source": [
    "xx3 = lambda n: n>1 and not any([i for i in range(2,int(n**.5)+1) if n%i==0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a22925db-482a-4165-a6c1-9b0f116543d3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ab4d586c-d709-4543-88e5-abd5ac52199b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i for i in range(-10,21) if xx1(i)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "51d03b28-7cb3-4db4-be19-4ff4dc250929",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i for i in range(-10,21) if xx2(i)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8d725cbd-7912-47cf-b4f9-33b6b8be9b87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i for i in range(-10,21) if xx3(i)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b43b6e81-03b6-4947-b61b-18a789f00f6d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02342596-306e-4a7e-81ce-4ec81a2aebee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
